package com.ccys.cms.controller.administrator;

import com.ccys.cms.service.SysCodeService;
import com.ccys.common.constants.ContextConstants;
import com.ccys.common.constants.TouristApi;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.SysCodeUtil;
import com.ccys.db.dto.web.base.SysCodeDto;
import com.ccys.db.entity.SysCode;
import com.ccys.db.vo.sysCodeCategory.SysCodeCategorySelectedListVo;
import io.swagger.annotations.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

import static com.ccys.common.util.Kit.allParamValid;
import static com.ccys.common.vo.ResponseResult.success;

/**
 * 系统参数
 */
@RestController
@RequestMapping("administrator")
@Api(tags = "系统设置-Application settings 系统参数")
public class SysCodeController {

    @Resource
    private SysCodeService sysCodeService;

    @ApiOperation("获取系统参数")
    @GetMapping(value = "/getCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "key", required = true, dataType = "string", paramType = "query"),
    })
    @TouristApi
    public ResponseResult<SysCode, Object> getCode(String key,String catId) {
        return success(SysCodeUtil.getCode(key,catId));
    }


    @GetMapping("sysCode")
    @ApiOperation("获取系统参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catId", value = "类型(Group)id", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "Keyword", paramType = "query"),
            @ApiImplicitParam(name = "isDev", value = "Dev", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = ContextConstants.MODEL_DESC, response = SysCode.class)
    })
    public ResponseResult list(@ApiIgnore SysCodeDto sysCodeDto) {
        return sysCodeService.queryCodeList(sysCodeDto);
    }


    @GetMapping("sysCode/listCategory")
    @ApiOperation("获取系统参数Category(Group 下拉选择)列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = ContextConstants.MODEL_DESC, response = SysCode.class)
    })
    public ResponseResult<SysCodeCategorySelectedListVo, Object> listCategory() {
        return sysCodeService.listCategory();
    }

    @PutMapping("sysCode")
    @ApiOperation("修改系统参数")
    @Transactional
    public ResponseResult updateCode(SysCode sysCode) {
        allParamValid(sysCode, "id");
        return this.sysCodeService.updateCode(sysCode);

    }


}
